package com.zoght.financial.model;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.sun.corba.se.impl.encoding.OSFCodeSetRegistry.Entry;

public class GenericReport {

	private List<TransactionBean> m_beans;
	
	public GenericReport(List<TransactionBean> txList) {
		assert(txList != null);
		m_beans = txList;
	}
	
	public void generate() {
		double externalInput = 0.0;
		double interest = 0.0;
		double dividend = 0.0;
		int buySellTx = 0;
		HashMap<String, StockInfoBean> records = new HashMap<String, StockInfoBean>();
		for (TransactionBean b : m_beans) {
		    if (b.getType() == TransactionBean.Type.TelephoneBanking) {
		    	externalInput += b.getTotal();
		    } else if (b.getType() == TransactionBean.Type.Interest) {
		    	interest += b.getTotal();
		    } else if (b.getType() == TransactionBean.Type.Dividend) {
		    	dividend += b.getTotal();
		    } else  {
		    	StockInfoBean currentRecord = records.get(b.getSymbol());
		    	if (currentRecord == null) {
		    		currentRecord = new StockInfoBean();
		    		currentRecord.setDescription(b.getDescription());
		    		currentRecord.setSymbol(b.getSymbol());
		    		records.put(b.getSymbol(), currentRecord);
		    	}
		    	currentRecord.inc(b.getType(), b.getUnits(), b.getPricePerUnit(), b.getTotal());
		    }
		}
		// tally
		for (Iterator iterator = records.entrySet().iterator(); iterator.hasNext();) {
			Map.Entry<String,StockInfoBean> entry = (Map.Entry<String,StockInfoBean>)iterator.next();
			entry.getValue().close();
			System.out.println(entry.toString());
		}
		// reporting
		double TotalCapitalGainLoss = 0;
		double TotalCost = 0;
		double RealizedCapitalGainLoss = 0;
		for (Iterator iterator = records.entrySet().iterator(); iterator.hasNext();) {
			Map.Entry<String,StockInfoBean> entry = (Map.Entry<String,StockInfoBean>)iterator.next();
			if (entry.getValue().getSymbol() != null) {
				if (entry.getValue().getStillHolding() == 0) {
				    RealizedCapitalGainLoss += entry.getValue().getRealizedGainsLosses();
				}
				TotalCapitalGainLoss += entry.getValue().getRealizedGainsLosses();
				TotalCost += entry.getValue().getCurrentCost();
			}
		}
		
		
		System.out.println("Realized Capital Gain/Loss: " + RealizedCapitalGainLoss + " Total GainLoss: " + TotalCapitalGainLoss + " , Total Cost = " + TotalCost);
		System.out.println("Interest: " + interest + " Dividend: " + dividend );		
		// Realized Capital Gains
	}
}
